package com.my.socket.bio.v5;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by ZhongGang
 * at 2018/6/28 14:46
 */
public class Client {
    private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);

    private String host;
    private int port;
    private Socket socket;

    public Client(String host, int port) throws Exception {
        this.host = host;
        this.port = port;

        this.socket = new Socket();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(this.host, this.port);
        this.socket.connect(inetSocketAddress);
    }

    public void write(String message) throws Exception {
        if (this.socket.isConnected()) {
            OutputStream outputStream = this.socket.getOutputStream();
            outputStream.write(message.getBytes());
        } else {
            LOGGER.error("no connection existed...");
        }
    }

    public static void main(String[] args) throws Exception {
        Client client = new Client("127.0.0.1", 9999);
        client.write("I am Alex Zhong...");
        client.write("我是Alex钟");

        while (true) {

        }
    }
}
